
//
//  AdviceController.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/22.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "AdviceController.h"
#import "JFBAdviceDS.h"

#define kAnimationDuration 0.2

@interface AdviceController ()<UITextViewDelegate,UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *adviceBtn;
@property (weak, nonatomic) IBOutlet UILabel *wordCountLabel;
@property (weak, nonatomic) IBOutlet UITextView *adviceTextView;

- (IBAction)saveAdviceAction:(id)sender;

@end

@implementation AdviceController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [self.adviceTextView setDelegate:self];
    //注册通知,监听键盘消失事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardWillHideNotification object:nil];
    
    [self.adviceBtn setBackgroundColor:ButtonBgColorImportant];
    self.adviceBtn.titleLabel.font = [UIFont systemFontOfSize:TextFontSizeH2];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
}

- (UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    
    return item;

}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)logout:(__unused id)sender {
    
}

// 键盘弹出时
-(void)keyboardDidShow:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        //获取键盘高度
        NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect;
        [keyboardObject getValue:&keyboardRect];
        //设置view的frame，往上平移
        [self.adviceBtn setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-kBottomBtnContainerHeight, self.view.bounds.size.width, kBottomBtnContainerHeight)];
    }];
    if( [self.adviceTextView.text isEqualToString:@"把你的建议告诉小piu~"] ) {
        self.adviceTextView.text = @"";
    }
}

-(void)keyboardDidHidden
{
    //设置view的frame，往下平移
    [self.adviceBtn setFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.bounds.size.width, 60)];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if ([textView.text length] > 256) {
        textView.text = [textView.text substringToIndex:256];
        [JFBUIAnimation alertTips:@"字数超出限制"];
    }
    
    NSString *nsTextContent = textView.text;
    
    NSUInteger existTextNum = [nsTextContent length];
    
    self.wordCountLabel.text = [NSString stringWithFormat:@"%lu",existTextNum];
}

- (IBAction)saveAdviceAction:(id)sender {
    if( [NSString isNilOrEmptyOrWhitespace:self.adviceTextView.text] ) {
        return;
    }
    
    [JFBAdviceDS advice:^(BOOL isSuccess, NSString *message, NSError *error) {
        [JFBUIAnimation alertTips:message];
        [self.navigationController popViewControllerAnimated:TRUE];
    } advice:self.adviceTextView.text];
    
}
//
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//    
////    [self.view endEditing:YES];
//    [self.adviceTextView resignFirstResponder];
//    
////    [super touchesBegan:touches withEvent:event];
//}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 0;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.adviceTextView resignFirstResponder];
}
//
//-(void)textViewDidEndEditing:(UITextView *)textView{
//    [textView resignFirstResponder];
//}
//
//
//-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
//    [textView resignFirstResponder];
//    
//    return YES;
//}

//- (void)textViewDidBeginEditing:(UITextView *)textView
//{
////    if (!textView.window.isKeyWindow) {
////        [textView.window makeKeyAndVisible];
////    }
//    
//    CGRect addStockRect = [textView convertRect: textView.frame toView: self.view];
//    CGFloat offSet = self.view.frame.size.height - (CGRectGetMaxY(addStockRect) + 216 + 40);
//    
//    NSTimeInterval animationDuration=0.30f;
//    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
//    
//    [UIView setAnimationDuration:animationDuration];
//    
//    if(offSet < 0)
//    {
//        self.view.frame=CGRectMake(0, offSet,self.view.frame.size.width, self.view.frame.size.height);
//        [UIView commitAnimations];
//    }
//    
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
